//
//  BYConfirmOrderController.m
//  OCDemo
//
//  Created by zhumuzhi on 2022/2/16.
//  确认订单

#import "BYConfirmOrderController.h"
#import "BYConfirmOrderAddressCell.h"
#import "BYConfirmOrderGoodsCell.h"

#import "BYAddressController.h"
#import "BYCashierController.h"

@interface BYConfirmOrderController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) BYTableView   *tableView;
// 立即支付
@property (nonatomic, strong) UIButton      *payNow;

@end

@implementation BYConfirmOrderController

static NSString *kBYConfirmOrderAddressCellId = @"BYConfirmOrderAddressCellId";
static NSString *kBYConfirmOrderGoodsCellId = @"BYConfirmOrderGoodsCellId";

#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"确认订单";
    self.view.backgroundColor = BACKGROUND_VIEW_COLOR;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.payNow];
    
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.payNow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-kAutoWithSize(10));
        make.bottom.equalTo(self.view).offset(-kAutoWithSize(50));
        make.width.mas_equalTo(kAutoWithSize(100));
        make.height.mas_equalTo(kAutoWithSize(40));
    }];
}

#pragma mark - Event
- (void)payNowEvent {
    BYCashierController *controller = [[BYCashierController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1 ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2 ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.row) {
        BYConfirmOrderAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:kBYConfirmOrderAddressCellId];
        return cell;
    }
    BYConfirmOrderGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:kBYConfirmOrderGoodsCellId];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.row) {
        return [BYConfirmOrderAddressCell cellHeight];
    }
    return [BYConfirmOrderGoodsCell cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.row) {
        BYAddressController *controller = [[BYAddressController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark - LazyGet

- (BYTableView *)tableView {
    if (!_tableView) {
        _tableView = [[BYTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kSafeAreaTopHeight-kSafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = BACKGROUND_VIEW_COLOR;
        [_tableView registerClass:[BYConfirmOrderAddressCell class] forCellReuseIdentifier:kBYConfirmOrderAddressCellId];
        [_tableView registerClass:[BYConfirmOrderGoodsCell class] forCellReuseIdentifier:kBYConfirmOrderGoodsCellId];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0.0f;
        }
    }
    return _tableView;
}

- (UIButton *)payNow {
    if (!_payNow) {
        _payNow = [[UIButton alloc] init];
        [_payNow setTitle:@"立即支付" forState:UIControlStateNormal];
        [_payNow setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        [_payNow setBackgroundColor:PUBLIC_ORANGE_COLOR forState:UIControlStateNormal];
        [_payNow setBackgroundColor:PUBLIC_ORANGE_COLOR forState:UIControlStateSelected];
        [_payNow setBackgroundColor:[UIColor colorWithHexString:@"#BBBBBB"] forState:UIControlStateDisabled];
        [_payNow setViewCornerRadius:kAutoWithSize(8)];
        [_payNow addTarget:self action:@selector(payNowEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _payNow;
}


@end
